Register-Lib PowerShell.Lang

filter InvokeOnCurrent($method) { 
    $private:target = $_
    if((-not ($method -is [string])) -and $method -is [System.Collections.IEnumerable]){
        $method | % { return $target | InvokeOnCurrent -method:$_}
    } else {
        return Invoke-Expression ('$target.' + $method)  
    }
    #$method | % {
    #    return Invoke-Expression ('$target.' + $_)  
    #}
}

filter Coalesce-Args($val) {
  if($_) { return $_ }
  else { return $val }
}

function Using2 {
    param (
        [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
        [ScriptBlock] $ProcessBlock = $(throw "The parameter -scriptBlock is required."),
        [ScriptBlock] $DisposeBlock
    )
    
    try {
        &$ProcessBlock
    } 
	finally {
        if ($inputObject -ne $null) {
            $private:disposable = $inputObject
        }
        else {
            $private:disposable = $inputObject.psbase
        }
        if($disposable -is [IDisposable]){
            try { 
                ([IDisposable]$disposable).Dispose()
            } catch {
                
            }
        }   
        if($DisposeBlock){
            $_ = $disposable
            & $DisposeBlock
        }
                 
    }
}